package com.kingbi.welcome.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BasicDynaBean;

import com.kingbi.welcome.dao.WelcomeDao;

public class WelcomeServiceImpl implements WelcomeService {
	WelcomeDao welcomeDao;

	public WelcomeDao getWelcomeDao() {
		return welcomeDao;
	}

	public void setWelcomeDao(WelcomeDao welcomeDao) {
		this.welcomeDao = welcomeDao;
	}

	public List listNotice() throws Exception {
		return null;
		
	}

	public int getCheckNum(String org_id,int rpt_year,int rpt_quarter,int status) throws Exception, SQLException {
		StringBuffer sqlBuffer = new StringBuffer();
		sqlBuffer.append(" select ms.appr_status from p_rpt_monitor rm");
		sqlBuffer.append(" inner join p_ent_industry ei on rm.industry_id=ei.industry_id");
		sqlBuffer.append(" left outer join c_moni_status ms on rm.rpt_id=ms.rpt_id and ms.org_id='");
		sqlBuffer.append(org_id);
		sqlBuffer.append("' and ms.rpt_year=");
		sqlBuffer.append(rpt_year);
		sqlBuffer.append(" and ms.rpt_quarter=");
		sqlBuffer.append(rpt_quarter);
		sqlBuffer.append(" where ei.org_id='");
		sqlBuffer.append(org_id);
		sqlBuffer.append("' and (ms.appr_status<>");
		sqlBuffer.append(status);
		sqlBuffer.append(" or ms.appr_status is null)");
		sqlBuffer.append(" union all");
		sqlBuffer.append(" select ms.appr_status from p_rpt_sum rm");
		sqlBuffer.append(" left outer join c_sum_status ms on rm.rpt_id=ms.rpt_id and ms.org_id='");
		sqlBuffer.append(org_id);
		sqlBuffer.append("' and ms.rpt_year=");
		sqlBuffer.append(rpt_year);
		sqlBuffer.append(" and ms.rpt_quarter=");
		sqlBuffer.append(rpt_quarter);
		sqlBuffer.append(" where 1=1");
		sqlBuffer.append(" and (ms.appr_status<>");
		sqlBuffer.append(status);
		sqlBuffer.append(" or ms.appr_status is null)");
		List list = welcomeDao.findBySQL(sqlBuffer.toString(), null);
		return list.size();
	}

	public List<HashMap> getStatusList(String org_id,int status) throws Exception, SQLException {
		List<HashMap> list = new ArrayList<HashMap>();
		StringBuffer sqlBuffer = new StringBuffer();
		sqlBuffer.append(" select rpt_year,rpt_quarter,'行业附表' name,min(appr_status) appr_status from c_moni_status ms");
		sqlBuffer.append(" where 1=1 ");
		sqlBuffer.append(" and ms.org_id='");
		sqlBuffer.append(org_id);
		sqlBuffer.append("' and ms.appr_status<>");
		sqlBuffer.append(status);
		sqlBuffer.append(" group by rpt_year,rpt_quarter");
		sqlBuffer.append(" union all");
		sqlBuffer.append(" select rpt_year,rpt_quarter,'监测总表' name,appr_status from c_sum_status ss");
		sqlBuffer.append(" where 1=1"); 
		sqlBuffer.append(" and ss.org_id='");
		sqlBuffer.append(org_id);
		sqlBuffer.append("' and ss.appr_status<>");
		sqlBuffer.append(status);
		sqlBuffer.append(" order by rpt_year,rpt_quarter");
		List result = welcomeDao.findBySQL(sqlBuffer.toString(), null);
		Iterator<Object> it = result.iterator();
		while(it.hasNext()){
			BasicDynaBean bd = (BasicDynaBean)it.next();
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("rpt_year", bd.get("rpt_year")==null?0:(Integer)bd.get("rpt_year"));
			map.put("rpt_quarter", bd.get("rpt_quarter")==null?0:(Integer)bd.get("rpt_quarter"));
			map.put("rpt_name", bd.get("name")==null?"":bd.get("name").toString());
			map.put("appr_status", bd.get("appr_status")==null?"":bd.get("appr_status").toString());
			list.add(map);
		}
		return list;
	}

	public boolean checkPeriod(int quarter) throws Exception, SQLException {
		Calendar c = Calendar.getInstance();
		int month = c.get(Calendar.MONTH)+1;
		int day = c.get(Calendar.DAY_OF_MONTH);
		StringBuffer sqlBuffer = new StringBuffer();
		sqlBuffer.append("select * from c_fill_period c where fill_term="+quarter); 
		sqlBuffer.append("and (("+month+">c.begin_month and "+month+"<c.end_month)");
		sqlBuffer.append("or ("+month+"=c.begin_month and "+day+">c.begin_day and "+month+"<c.end_month)");
		sqlBuffer.append("or ("+month+"=c.begin_month and "+day+">c.begin_day and "+month+"=c.end_month and "+day+"<c.end_day)");
		sqlBuffer.append("or ("+month+">c.begin_month and "+month+"=c.end_month and "+day+"<c.end_day))");
		List result = welcomeDao.findBySQL(sqlBuffer.toString(), null);
		if(result.size()>0){//在填报期
			return true;
		}else{//不在填报期
			return false;
		}
		
	}
}
